package cn.mall.product.controller;

import cn.mall.common.utils.PageUtils;
import cn.mall.common.utils.R;
import cn.mall.product.entity.AttrGroupEntity;
import cn.mall.product.service.AttrGroupService;
import cn.mall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * 属性分组
 *
 * @author ssd
 * @email 18336719215@163.com
 * @date 2023-10-19 11:04:18
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
@Autowired
private CategoryService categoryService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }
    /**
     * 根据三级分类ID查询父类下的属性分组
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable Long catelogId){
        PageUtils page = attrGroupService.queryPageBylist(params, catelogId);
        return R.ok().put("page", page);
    }
    /**
     * 查询属性分组
     * @param attrGroupId 属性分组ID
     * @return 属性分组信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        //查找三级分类的完整路径 [level1Id, level2Id, level3Id]
        Long[] catelogPath =categoryService.findCategoryPath(attrGroup.getCatelogId());
        //TODO
//        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }




    /**
     * 信息
     */
//    @RequestMapping("/info/{attrGroupId}")
//    public R info(@PathVariable("attrGroupId") Long attrGroupId){
//		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
//
//        return R.ok().put("attrGroup", attrGroup);
//    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
